package oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;

import oa.entity.Department;
import oa.entity.Login;
import oa.entity.User;
import oa.exception.DaoException;
import oa.paging.Page;
import oa.service.CompanyService;
import oa.service.DepartmentService;
import oa.service.LoginService;
import oa.service.UserService;
import oa.service.impl.CompanyServiceImpl;
import oa.service.impl.DepartmentServiceImpl;
import oa.service.impl.LoginServiceImpl;
import oa.service.impl.UserServiceImpl;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		UserService userService = new UserServiceImpl();
		DepartmentService departmentService = new DepartmentServiceImpl();
		User us = new User();
		Page<User> pageInfo = new Page<User>();
		List<User> list = new ArrayList<User>() ;
		String pageNo = request.getParameter("PageNo");
		if(pageNo==null){
			pageNo="1";
		}
		boolean flag = false;	
		String opt = request.getParameter("opt");
	    String id=request.getParameter("userid");
	    int count=0;
	    pageInfo.setCurrentPage(Integer.parseInt(pageNo));
	    pageInfo.setPageSize(8);
	    //根据id添加
	    if("useradd".equals(opt)) {
			try {
				String username=request.getParameter("val-username");
				String email = request.getParameter("val-email");
				String loginname= request.getParameter("val-loginname");
				String loginpwd = request.getParameter("val-loginpwd");
				int sex = Integer.parseInt(request.getParameter("val-sex"));
				String phone=request.getParameter("val-phone");
				String aress =request.getParameter("val-suggestions");
				String  currency =request.getParameter("val-currency");
				String familyphone=request.getParameter("val-familyphone");
				String userdate = request.getParameter("val-date");
				Date date = new SimpleDateFormat("yyyy-MM-dd").parse(userdate);
			    String diploma = request.getParameter("val-diploma");
			    String digits= request.getParameter("val-digits");
			    int comanyid = Integer.parseInt(request.getParameter("val-company"));
			    int  deptid= Integer.parseInt(request.getParameter("val-dept"));			   
		        us.setUserName(username);
		        us.setLoginName(loginname);
		        us.setLoginPwd(loginpwd);
		        us.setUserSex(sex);
		        us.setUserPhone(phone);
		        us.setUserEmail(email);
		        us.setUserAddress(aress);
		        us.setUserIdentitycard(currency);
		        us.setUserFamilyphone(familyphone);
		        us.setUserDiploma(diploma);
		        us.setDeptId(deptid);
		        us.setUserPosition(digits);    
		        us.setUserEntrytime(date);
		        us.setCompanyId(comanyid);
		        
		        flag =userService.addUser(us);
		       
			} catch (Exception e) {
				// TODO: handle exception
			}
	       out.print(JSON.toJSONString(flag));
		} 
		//根据id获取所有信息
		else if("selectuser".equals(opt)) { 
			try {
				Integer userid = Integer.parseInt(id);
				list=userService.selectUser(userid,null,0);
				session.setAttribute("userlist", list);
				request.getRequestDispatcher("user_info.jsp").forward(request, response);
			} catch (Exception e) {
				// TODO: handle exception
			}
		//根据id获取到信息	
		}else if("upuser".equals(opt)) {
			try {
				Integer userid = Integer.parseInt(id);
				out.print(userid);
				list=userService.selectUser(userid,null,0);
				session.setAttribute("userlist", list);
				request.getRequestDispatcher("user_modify.jsp").forward(request, response);
			} catch (Exception e) {
				// TODO: handle exception
			}
	    //根据id进行更新
		}else if("UpDateUser".equals(opt)) {
			boolean yz = false;
			int sex =0;
			try {
				String userid = request.getParameter("val-userid");
				String username=request.getParameter("val-username");
				String email = request.getParameter("val-email");
				String loginname= request.getParameter("val-loginname");
				String loginpwd = request.getParameter("val-loginpwd");
			    String  xb = request.getParameter("val-sex");
			    if("男".equals(xb)) {
			    	sex =1;
			    }else {
			    	sex =2;
			    }
				String phone=request.getParameter("val-phone");
				String aress =request.getParameter("val-aress");
				String  userIdentitycard =request.getParameter("val-userIdentitycard"); 
				String familyphone=request.getParameter("val-familyphone");
				String userdate = request.getParameter("val-date");
				Date date = new SimpleDateFormat("yyyy-MM-dd").parse(userdate);
			    String diploma = request.getParameter("val-userdiploma");
			    String position= request.getParameter("val-position");
			    int comanyid = Integer.parseInt(request.getParameter("val-company"));
			    int  deptid= Integer.parseInt(request.getParameter("val-dept"));
			    User uss = new User(Integer.parseInt(userid),username,loginname,loginpwd,sex,phone,email,aress,userIdentitycard,familyphone,diploma,deptid,position,date,comanyid);		      
		        yz =userService.updateUser(uss);
		        
			} catch (Exception e) {
				// TODO: handle exception
			}
			out.print(JSON.toJSONString(yz));
			
			
		}else if("del".equals(opt)) {//根据id删除
			flag = userService.deleteUser(Integer.parseInt(request.getParameter("userid")));
			out.print(JSON.toJSONString(flag));
			
	   //最终执行
		} else {
			
			List<User> userList =  new ArrayList<User>();
			int companyid = (int)session.getAttribute("companyid");
		    String companyidd =request.getParameter("companyid");
		    int gongsid = 0;
		    if(companyidd==null) {
		    	companyidd="0";
		    }
			try {
			//默认根据当前所登陆的公司id查询
			
			if(companyid==0 && "0".equals(companyidd)) {
				userList = userService.selectAllUser(null,pageInfo.getCurrentPage(), pageInfo.getPageSize());
				count = userService.selectAllUserCount(0,0);
				gongsid = 0;
			}else if(companyid!=0 && companyidd!="0") {
				gongsid = Integer.parseInt(companyidd);
				us.setCompanyId(Integer.parseInt(companyidd));
				userList = userService.selectAllUser(us,pageInfo.getCurrentPage(), pageInfo.getPageSize());
				count = userService.selectAllUserCount(0,Integer.parseInt(companyidd));
			}else if(companyidd!="0") {
				gongsid = Integer.parseInt(companyidd);
				us.setCompanyId(Integer.parseInt(companyidd));
				userList = userService.selectAllUser(us,pageInfo.getCurrentPage(), pageInfo.getPageSize());
				count = userService.selectAllUserCount(0,Integer.parseInt(companyidd));
				
			}else {
				gongsid = companyid;
				us.setCompanyId(companyid);
				userList = userService.selectAllUser(us,pageInfo.getCurrentPage(), pageInfo.getPageSize());
				count = userService.selectAllUserCount(0,companyid);
				
			}
			List<Department> deptList = departmentService.selectAllDepartment(0,0, 0);						
			pageInfo.setTotalRow(count);
			pageInfo.setPageList(userList);
		    session.setAttribute("pageInfo", pageInfo);
			session.setAttribute("deptList", deptList);	
			session.setAttribute("company", gongsid);
			request.getRequestDispatcher("user_list.jsp").forward(request, response);
		} catch (Exception e) {
			
		}	
		
		}
	
		out.flush();
		out.close();

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
